package org.easy.baiducensor;

import com.baidu.aip.contentcensor.AipContentCensor;
import com.baidu.aip.contentcensor.EImgType;
import org.easy.baiducensor.common.BaiduCensorResponse;
import org.easy.baiducensor.enums.AuditResultType;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * 通用方法模板
 */

public class BaiduCensorTemplate {


    private BaiduCensorClient baiduCensorClient;

    public BaiduCensorTemplate(BaiduCensorClient baiduCensorClient) {
        this.baiduCensorClient = baiduCensorClient;
    }


    /// 文本审核

    public BaiduCensorResponse<List<String>> baiduTextCensor(String text) {
        ///构建返回值对象
        BaiduCensorResponse<List<String>> baiduCensorResponse = new BaiduCensorResponse<>();

        AipContentCensor connection = baiduCensorClient.getConnection();
        ///调用文本审核
        JSONObject response = connection.textCensorUserDefined(text);
        Integer conclusionType = response.getInt("conclusionType");
        if (conclusionType.equals(AuditResultType.AUDIT_FAILED.getCode())) {
            ///审核失败直接结束
            baiduCensorResponse.setCode(conclusionType);
            baiduCensorResponse.setMsg("审核失败！请人工核查！");
            return baiduCensorResponse;
        }
        String conclusion = response.getString("conclusion");
        List<String> list = new ArrayList<>();
        if (conclusionType.equals(AuditResultType.NON_COMPLIANT.getCode()) || conclusionType.equals(AuditResultType.SUSPECTED.getCode())) {
            ///不合规的信息传过去
            Object data = response.get("data");
            JSONArray dataArray = (JSONArray) data;
            dataArray.forEach(e -> {
                JSONObject jsonObject = (JSONObject) e;
                Object hits = jsonObject.get("hits");
                JSONArray hitsArray = (JSONArray) hits;
                hitsArray.forEach(c -> {
                    JSONObject word = (JSONObject) c;
                    Object words = word.get("words");
                    if (Objects.nonNull(words)) {
                        JSONArray wordArray = (JSONArray) words;
                        wordArray.forEach(w -> {
                            list.add(w.toString());
                        });
                    }
                });
            });
            baiduCensorResponse.setData(list);
        }
        baiduCensorResponse.setMsg(conclusion);
        baiduCensorResponse.setCode(conclusionType);
        return baiduCensorResponse;

    }

    /// 图片审核
    public BaiduCensorResponse<List<String>> baiduImageCensor(List<String> image) {
        ///创建返回值
        BaiduCensorResponse<List<String>> baiduCensorResponse = new BaiduCensorResponse<>();
        AipContentCensor connection = baiduCensorClient.getConnection();
        for (String url : image) {
            ///调用审核接口
            JSONObject response = connection.imageCensorUserDefined(url, EImgType.URL, null);
            Integer conclusionType = response.getInt("conclusionType");
            String conclusion = response.getString("conclusion");
            if (conclusionType.equals(AuditResultType.AUDIT_FAILED.getCode())) {
                ///审核失败直接结束
                baiduCensorResponse.setMsg("审核失败！请人工核查！");
                return baiduCensorResponse;
            }

            if (conclusionType.equals(AuditResultType.NON_COMPLIANT.getCode())) {
                List<String> list = new ArrayList<>();
                ///不合规的信息传过去
                Object data = response.get("data");
                JSONArray dataArray = (JSONArray) data;
                dataArray.forEach(c -> {
                    JSONObject msg = (JSONObject) c;
                    String info = msg.getString("msg");
                    list.add(info);
                });
                baiduCensorResponse.setData(list);
            }
            baiduCensorResponse.setCode(conclusionType);
            baiduCensorResponse.setMsg(conclusion);
        }

        return baiduCensorResponse;
    }


    /// 音频审核
    public String baiduVoiceCensorOfWav(String url) {
        AipContentCensor connection = baiduCensorClient.getConnection();
        ///可选参数配置
        HashMap<String, Object> options = new HashMap<>();
        options.put("rawText", "true");
        options.put("split", "true");

        Integer rate = 16000;
        JSONObject jsonObject = connection.voiceUrlCensorUserDefined(url, rate, "wav", options);
        return jsonObject.toString();

    }

    /// 短视频审核
    public String baiduVideoCensor(String url, String name, String extId) {
        AipContentCensor connection = baiduCensorClient.getConnection();
        JSONObject jsonObject = connection.videoCensorUserDefined(name, url, extId, null);
        return jsonObject.toString();
    }
    /// 短视频审核
    public BaiduCensorResponse<List<String>> baiduShortVideoCensor(String url) {
        ///创建返回值
        BaiduCensorResponse<List<String>> baiduCensorResponse = new BaiduCensorResponse<>();
        AipContentCensor connection = baiduCensorClient.getConnection();
        String extId = String.valueOf(System.currentTimeMillis());
        String videoName = getVideoName(url);
        JSONObject response = connection.videoCensorUserDefined(videoName, url, extId, null);
        Integer conclusionType = response.getInt("conclusionType");
        String conclusion = response.getString("conclusion");
        if (conclusionType.equals(AuditResultType.AUDIT_FAILED.getCode())|| conclusionType.equals(AuditResultType.SUSPECTED.getCode())) {
            ///审核失败直接结束
            baiduCensorResponse.setMsg("审核失败！请人工核查！");
            return baiduCensorResponse;
        }

        if (conclusionType.equals(AuditResultType.NON_COMPLIANT.getCode())) {
            List<String> list = new ArrayList<>();
            ///不合规的信息传过去
            Object data = response.get("data");
            JSONArray dataArray = (JSONArray) data;
            dataArray.forEach(c -> {
                JSONObject msg = (JSONObject) c;
                String info = msg.getString("msg");
                list.add(info);
            });
            baiduCensorResponse.setData(list);
        }
        baiduCensorResponse.setCode(conclusionType);
        baiduCensorResponse.setMsg(conclusion);
        return baiduCensorResponse;
    }

    private String getVideoName(String url) {
        ///http:  .wav(fileOss上的访问路径)
        int lastIndexOf = url.lastIndexOf('.');
        int lastIndexOf1 = url.lastIndexOf('/');
        return url.substring(lastIndexOf1 + 1, lastIndexOf);
    }

    /// 长视频审核
    public String baiduLongVideoCensor(String url, String extId) {
        AipContentCensor connection = baiduCensorClient.getConnection();
        JSONObject jsonObject = connection.longVideoCensorSubmit(url, extId, null);
        return jsonObject.toString();
    }

    /// 长音频审核
    public BaiduCensorResponse<List<String>> baiduVoiceCensorOfMp3(String url) {
        ///创建返回值类型
        BaiduCensorResponse<List<String>> baiduCensorResponse = new BaiduCensorResponse<>();
        AipContentCensor connection = baiduCensorClient.getConnection();
        Integer rate = 16000;
        String fmt = "mp3";
        JSONObject response = connection.asyncVoiceCensorSubmit(url, fmt, rate, null);
        Integer conclusionType = response.getInt("conclusionType");
        if (conclusionType.equals(AuditResultType.AUDIT_FAILED.getCode())) {
            ///审核失败直接结束
            baiduCensorResponse.setCode(conclusionType);
            baiduCensorResponse.setMsg("审核失败！请人工核查！");
            return baiduCensorResponse;
        }
        String conclusion = response.getString("conclusion");
        if (conclusionType.equals(AuditResultType.NON_COMPLIANT.getCode())) {
            List<String> list = new ArrayList<>();
            ///不合规的信息传过去
            Object data = response.get("data");
            JSONArray dataArray = (JSONArray) data;
            dataArray.forEach(c -> {
                JSONObject msg = (JSONObject) c;
                String info = msg.getString("msg");
                list.add(info);
            });
            baiduCensorResponse.setData(list);
        }
        baiduCensorResponse.setCode(conclusionType);
        baiduCensorResponse.setMsg(conclusion);
        return baiduCensorResponse;
    }
}



